<?php

class CustomerForm extends BaseForm {

    public function __construct($array = null) {
        if (!$array)
            $this->contactsArr = array('activity' => 1,'owner' => 1, 'consultant' => 1, 'key_person' => 1, 0 => 1);
        else
            $this->contactsArr = $array;
        parent::__construct();
    }

    public function setup() {
        $titles = $this->getTitlesFormatted();

        $this->setWidgets(array(
            'title_id' => new sfWidgetFormChoice(array('choices' => $titles)),
            'customer_name' => new sfWidgetFormInputText(),
            'shipping_address' => new sfWidgetFormInputText(),
            'billing_address' => new sfWidgetFormInputText(),
            'email' => new sfWidgetFormInputText(),
            'notes' => new sfWidgetFormTextarea(),
            'payment_history' => new sfWidgetFormChoice(array('choices' => array('poor' => 'poor', 'good' => 'good', 'excellent' => 'excellent'))),
            'payment_method' => new sfWidgetFormChoice(array('choices' => array('cash' => 'cash', 'cheque' => 'cheque', 'open account' => 'open account')))
        ));

        $this->setValidators(array(
            'title_id' => new sfValidatorChoice(array('choices' => array_keys($titles), 'required' => true), array('invalid' => 'Invalid Title.', 'required' => 'Please enter required field.')),
            'customer_name' => new sfValidatorString(array('required' => true), array('required' => 'Please enter required field.')),
            'shipping_address' => new sfValidatorString(array('required' => true), array('required' => 'Please enter required field.')),
            'billing_address' => new sfValidatorString(array('required' => true), array('required' => 'Please enter required field.')),
            'email' => new sfValidatorRegex(array('required' => false, 'pattern' => '/^([a-zA-Z0-9_\-\.]+)@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/'), array('invalid' => 'Invalid E-mail.', 'required' => 'Please enter required field.')),
            'notes' => new sfValidatorString(array('required' => false), array("required" => "Please type your notes.")),
            'payment_history' => new sfValidatorChoice(array('choices' => array_keys(array('poor' => 'poor', 'good' => 'good', 'excellent' => 'excellent')), 'required' => true), array('required' => 'Please enter required field.')),
            'payment_method' => new sfValidatorChoice(array('choices' => array_keys(array('cash' => 'cash', 'cheque' => 'cheque', 'open account' => 'open account')), 'required' => true), array('required' => 'Please enter required field.'))
        ));

        $this->mergeForm(new ContactForm('_owner', $this->contactsArr['owner']));
        $this->mergeForm(new ContactForm('_consultant', $this->contactsArr['consultant']));
        $this->mergeForm(new ContactForm('_key_person', $this->contactsArr['key_person']));

        for ($i = 0; $i < (count($this->contactsArr) - 4); $i++) {
            $this->mergeForm(new ContactForm('_contact_' . $i, $this->contactsArr[$i]));
        }

        for ($i = 0; $i < $this->contactsArr['activity']; $i++) {
            $this->mergeForm(new ActivityForm('_' . $i));
        }

        $this->widgetSchema->setNameFormat('customer[%s]');
    }

    private function getTitlesFormatted() {
        $bank_accounts = Doctrine_Query::create()->from('Title')->fetchArray();
        $result = array('' => '');

        foreach ($bank_accounts as $bank_account) {
            $result[$bank_account['id']] = $bank_account['title_name'];
        }
        return $result;
    }

}

?>
